package util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConnectionUtil {


	private static final String IP_ADDRESS = "192.168.139.189";
	private static final int PORT = 5672;//RabbitMQ 服务端默认端口号为 5672

	/**
	 *   获取mq的链接  定义一个链接工厂
	 * @return
	 * @throws IOException
	 * @throws TimeoutException
	 */
	public static Connection  getConnection() throws IOException, TimeoutException{
		//定义一个连接工厂
		ConnectionFactory factory =new ConnectionFactory();

		//设置服务地址
		factory.setHost(IP_ADDRESS);

		//AMQP 5672
		factory.setPort(PORT);
		//vhost
		factory.setVirtualHost("sunfeng");
		//用户名
		factory.setUsername("admin");
		//密码
		factory.setPassword("123456");
		return factory.newConnection();
	}
	public static Channel getChannel() throws IOException, TimeoutException{

		return  getConnection().createChannel();
	}
}
